﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer.App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_HastenMind : Buff, IConsumable
    {
        private int PercentAdded = 10;

        public Spell_HastenMind()
        {
            minutesLeft = 1;
        }

        public override bool castWhileAlone(SpellIcon sIcon)
        {
            return true;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            PercentAdded = PercentAdded + (castLevel / 2);

            if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.CastingSpeedPercentModifier = TargetCreatureBase.CastingSpeedPercentModifier - PercentAdded;
            else if (spellIcon.Skill == "Shadow")
                TargetCreatureBase.MovementSpeedPercentModifier = TargetCreatureBase.MovementSpeedPercentModifier - PercentAdded;
            
            spellTimer.Start();
        }
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            PercentAdded = PercentAdded + (CastLevel / 2);
            minutesLeft = System.Convert.ToInt32(Math.Floor(myCreatureBase.WisdomTotal / 5.00));

            if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.CastingSpeedPercentModifier = TargetCreatureBase.CastingSpeedPercentModifier - PercentAdded;
            else if (spellIcon.Skill == "Shadow")
                TargetCreatureBase.MovementSpeedPercentModifier = TargetCreatureBase.MovementSpeedPercentModifier - PercentAdded;

            TargetCreatureBase.buffManager.UpdateBuffs();

            //Start Spell Timer
            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.CastingSpeedPercentModifier = TargetCreatureBase.CastingSpeedPercentModifier + PercentAdded;
            else if (spellIcon.Skill == "Shadow")
                TargetCreatureBase.MovementSpeedPercentModifier = TargetCreatureBase.MovementSpeedPercentModifier + PercentAdded;
        }

        //Run Speed Alchemy Potion
        public bool MeetsConsumeRequirements(Character character, Loot lootitem)
        {
            return true;
        }

        public void Consume(Character myCharacter, Loot lootitem)
        {
            TargetCreatureBase = myCharacter;

            minutesLeft = 10;

            LockItem lockItem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockItem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - myCharacter.MapX) <= 8 && Math.Abs(mapitem.MapY - myCharacter.MapY) <= 8)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                            {
                                Character character = (Character)creaturebase;
                                character.PlaySound("DrinkBottle", myCharacter.MapX, myCharacter.MapY);
                                character.AddCreatureBase(myCharacter);
                            }
                        }
                    }
                }

                SpellIcon potion = new SpellIcon();
                this.SpellID = TargetCreatureBase.GameHandler.ItemIDGen.GetSpellID();
                potion.ClassName = lootitem.ClassName;
                potion.Description = lootitem.Description;
                potion.ImageURL = lootitem.ImageURL;
                potion.Name = lootitem.Name;
                potion.Offensive = false;
                potion.CastType = "Self";
                potion.StackType = "MovementHaste";

                this.spellIcon = potion;
                this.SpellID = Managers.GameHandler.ItemIDGen.GetSpellID();

                this.SpellID = TargetCreatureBase.GameHandler.ItemIDGen.GetSpellID();
                this.spellIcon = potion;

                PercentAdded = 20;
                TargetCreatureBase.MovementSpeedPercentModifier = TargetCreatureBase.MovementSpeedPercentModifier - PercentAdded;

                //Remove existing recall potions
                foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
                {
                    if (buff.spellIcon.StackType == base.spellIcon.StackType)
                    {
                        buff.RemoveSpell(false, false);
                    }
                }
                TargetCreatureBase.buffManager.AddBuff(this);
                TargetCreatureBase.buffManager.UpdateBuffs();


                spellTimer.Start();
            }
        }
    }
}
